package top.lyc829.mymusic.ui.fragment.index

import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.lzx.starrysky.StarrySky
import com.youth.banner.indicator.CircleIndicator
import top.lyc829.mymusic.R
import top.lyc829.mymusic.adapter.BannerAdapter
import top.lyc829.mymusic.adapter.IndexAlbumAdapter
import top.lyc829.mymusic.adapter.IndexRecommendPlayListAdapter
import top.lyc829.mymusic.base.BaseFragment
import top.lyc829.mymusic.databinding.FragmentIndexBinding
import top.lyc829.mymusic.ext.dismissLoading
import top.lyc829.mymusic.ext.showLoading
import top.lyc829.mymusic.model.bean.AlbumOrSongBean
import top.lyc829.mymusic.model.bean.TypeEnum

class IndexFragment : BaseFragment<IndexViewModel, FragmentIndexBinding>() {

    private val adapter: IndexRecommendPlayListAdapter by lazy { IndexRecommendPlayListAdapter() }
    private val albumAdapter: IndexAlbumAdapter by lazy { IndexAlbumAdapter() }
    private val songAdapter: IndexAlbumAdapter by lazy { IndexAlbumAdapter() }

    companion object {
        fun newInstance() = IndexFragment()
        const val TAG = "IndexFragment"
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.toolbar.toolbar.run {
            title = "首页"
            inflateMenu(R.menu.index_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.index_search -> {
                        findNavController().navigate(R.id.action_mainFragment_to_searchFragment)
                    }
                }
                true
            }
        }

        binding.bannerView.apply {
            addBannerLifecycleObserver(this@IndexFragment)//添加生命周期观察者
            setAdapter(BannerAdapter(null))
            indicator = CircleIndicator(context)
        }


        val manager = GridLayoutManager(context, 3)
        manager.orientation = GridLayoutManager.VERTICAL
        binding.rvPlaylist.layoutManager = manager
        adapter.animationEnable = true
        binding.rvPlaylist.adapter = adapter

        val manager2 = GridLayoutManager(context, 3)
        manager2.orientation = GridLayoutManager.VERTICAL
        binding.rvDiscoverAlbumSong.layoutManager = manager2
        adapter.animationEnable = true
        binding.rvDiscoverAlbumSong.adapter = albumAdapter
    }

    override fun initClickListener() {
//        binding.button.setOnClickListener {
//            val info = SongInfo()
//            info.songId = "1625405815073"
//            info.songUrl = "http://m8.music.126.net/20210704220201/0508458a851720fe95894c15964f74b5/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/8196201777/a9b3/111a/4bae/085a26c4f81513dcdc420e43053449d0.mp3"
//            StarrySky.with().playMusicByInfo(info)
//        }

        adapter.setOnItemClickListener { _, _, position ->
            val id: Long = adapter.getItem(position).id
            val copywriter = adapter.getItem(position).copywriter
            val bundle = Bundle()
            bundle.putLong("id", id)
            bundle.putString("copywriter", copywriter)
            findNavController().navigate(R.id.action_mainFragment_to_playListDetailFragment, bundle)
        }

        binding.tvDiscoverNewAlbum.setOnClickListener {
            mViewModel.bottomType.value = TypeEnum.ALBUM
        }
        binding.tvDiscoverNewSong.setOnClickListener {
            mViewModel.bottomType.value = TypeEnum.SONG
        }

        binding.tvDiscoverGedanSquare.setOnClickListener {
            findNavController().navigate(R.id.action_mainFragment_to_playListGroundFragment)
        }

    }

    override fun initData() {
        mViewModel.getBanners()
        mViewModel.getPlayList()
        mViewModel.getTopAlbums()
        mViewModel.getNewSongs()
    }

    override fun createObserver() {
        mViewModel.isLoading.observe(viewLifecycleOwner) {
            if (it) showLoading() else dismissLoading()
        }
        mViewModel.bannerList.observe(viewLifecycleOwner) {
            binding.bannerView.adapter.setDatas(it)
            binding.bannerView.setCurrentItem(1)
        }
        mViewModel.playList.observe(viewLifecycleOwner) {
            adapter.setList(it)
        }

        mViewModel.bottomType.observe(viewLifecycleOwner) {
            when(it) {
                TypeEnum.ALBUM -> {
                    binding.rvDiscoverAlbumSong.adapter = albumAdapter
                    albumAdapter.notifyDataSetChanged()
                    binding.tvDiscoverMoreAlbumSong.text = "更多新碟"
                    binding.tvDiscoverNewAlbum.textSize = 15F
                    binding.tvDiscoverNewAlbum.setTextColor(resources.getColor(R.color.black, null))
                    binding.tvDiscoverNewAlbum.typeface = Typeface.DEFAULT_BOLD
                    binding.tvDiscoverNewSong.textSize = 12F
                    binding.tvDiscoverNewSong.setTextColor(resources.getColor(R.color.gray, null))
                    binding.tvDiscoverNewSong.typeface = Typeface.DEFAULT

                }
                TypeEnum.SONG -> {
                    binding.rvDiscoverAlbumSong.adapter = songAdapter
                    songAdapter.notifyDataSetChanged()
                    binding.tvDiscoverMoreAlbumSong.text = "新歌推荐"
                    binding.tvDiscoverNewAlbum.textSize = 12F
                    binding.tvDiscoverNewAlbum.setTextColor(resources.getColor(R.color.gray, null))
                    binding.tvDiscoverNewAlbum.typeface = Typeface.DEFAULT
                    binding.tvDiscoverNewSong.textSize = 15F
                    binding.tvDiscoverNewSong.setTextColor(resources.getColor(R.color.black, null))
                    binding.tvDiscoverNewSong.typeface = Typeface.DEFAULT_BOLD

                }
                else -> {}
            }
        }

        mViewModel.topAlbums.observe(viewLifecycleOwner) {  datas ->
            val result: List<AlbumOrSongBean> = datas.map {
                AlbumOrSongBean(it.id, TypeEnum.ALBUM, it.picUrl, it.name , it.artists[0].name)
             }
            albumAdapter.setList(result)

        }

        mViewModel.newSongs.observe(viewLifecycleOwner) { datas ->
            val result: List<AlbumOrSongBean> = datas.map {
                AlbumOrSongBean(it.id, TypeEnum.SONG, it.album.picUrl, it.name, it.artists[0].name)
            }
            songAdapter.setList(result)
        }

        mViewModel.errorLiveData.observe(this) {
            ToastUtils.showShort(it.errorMsg)
        }

    }

    override fun onDestroy() {
        StarrySky.with().stopMusic()
        super.onDestroy()
    }

    override fun getViewModel() = ViewModelProvider(this).get(IndexViewModel::class.java)
    override fun getBinding(inflater: LayoutInflater) = FragmentIndexBinding.inflate(inflater)

}